//
//  RechargeController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/3.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "RechargeController.h"
#import "RechargeTypeAlertController.h"
#import "PayOrderTool.h"
@interface RechargeController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *amountTextField;
@property (weak, nonatomic) IBOutlet UIImageView *payTypeImageView;
@property (weak, nonatomic) IBOutlet UILabel *payTypeNameLabel;
@property (nonatomic,strong) NSString *paytype;
@end

@implementation RechargeController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [Center addObserver:self selector:@selector(paySuccess) name:@"PaySuccess" object:nil];
}
- (void)paySuccess {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)setupUI {
    self.title = @"充值";
    [self.leftbutton setImage:imgname(@"lefticon") forState:(UIControlStateNormal)];
     self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    self.amountTextField.delegate = self;
    self.paytype = @"0";
}
- (IBAction)payTypeSelectButtonClick:(UIButton *)sender {
    RechargeTypeAlertController * vc = [[RechargeTypeAlertController alloc] init];
    vc.selectedPayTypeClick = ^(NSString * _Nonnull type) {
        if ([type isEqualToString:@"0"]) {
            self.payTypeImageView.image = [UIImage imageNamed:@"微信支付图标"];
            self.payTypeNameLabel.text = @"微信支付";
            
        } else {
            self.payTypeImageView.image = [UIImage imageNamed:@"支付宝图标"];
            self.payTypeNameLabel.text = @"支付宝支付";
        }
        self.paytype = type;
    };
    [self presentViewController:vc animated:YES completion:nil];
}
- (IBAction)confirmButtonClick:(UIButton *)sender {
    if ([self.amountTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入充值金额");
        return;
    }
    if ([self.paytype isEqualToString:@"0"]) {// 微信充值
        
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"userId"] = USER_ID;
        param[@"money"] = self.amountTextField.text;
        [MBProgressHUD showHUDAddedTo:self.view  animated:YES];
        [NetWorkTool POST:orderWxPay param:param success:^(id dic) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [PayOrderTool wxPay:[dic objectForKey:@"data"]];
        } other:^(id dic) {
            ShowToastWithText(dic[@"msg"]);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        } fail:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        } needUser:YES];
    } else {// 支付宝充值
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"userId"] = USER_ID;
        param[@"money"] = self.amountTextField.text;
        [MBProgressHUD showHUDAddedTo:self.view  animated:YES];
        [NetWorkTool POST:orderAliPay param:param success:^(id dic) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [PayOrderTool aliPay:[dic objectForKey:@"data"]];
        } other:^(id dic) {
            ShowToastWithText(dic[@"msg"]);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        } fail:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        } needUser:YES];
    }
}
#pragma UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (string.length == 0) {
        return YES;
    }
    NSString *checkStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    //正则表达式（只支持两位小数）
    NSString *regex = @"^\\-?([1-9]\\d*|0)(\\.\\d{0,2})?$";
    //判断新的文本内容是否符合要求
    return [self isValid:checkStr withRegex:regex];
}
- (BOOL) isValid:(NSString*)checkStr withRegex:(NSString*)regex
{
    NSPredicate *predicte = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicte evaluateWithObject:checkStr];
}
@end
